<?php
class ProductAction extends AdminBaseAction {

	public function index() {
		import('ORG.Util.Page');       //导入分页类
        $product   =   M('Product');
		$condition['disabled']='false';
        $count  = $product->where($condition)->count();    //计算总数
        $Page = new Page($count, C('PAGE_NUM'));
        $list   = $product->limit($Page->firstRow. ',' . $Page->listRows)->where($condition)->order('id desc')->select();
        $Page->setConfig('header', '条数据');
        $Page->setConfig('first', '<<');
        $Page->setConfig('last', '>>');
        $page = $Page->show();
		$this->assign("count",$count);
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->display();
	}
	
	public function showadd(){
		$this->display();
	}

	public function add(){
		$product=M('Product');
		$condition['name']=$_POST['name'];
		$condition['code']=$_POST['code'];
		$condition['_logic']='or';
		$list=$product->where($condition)->select();
		if(count($list)>0){
			$this->error('此产品已经存在不能重复添加');
		}
		$result=$product->add($_POST);
		
		if($result){
			$this->success('添加成功');
			$this->redirect('Product/index');
		}else{
			$this->error('添加错误');
		}
	}

	public function recycle(){
		$map['id']  = array('in',$_POST['values']);
		$product=M('Product');
		$data['disabled']='true';
		$product->where($map)->save($data);
		$this->redirect('Product/index');
	}
	
	public function edit(){
		$map['id']=$_GET['id'];
		if(!empty($map['id'])){
			$product=M('Product');
			$list=$product->where(array('id'=>$map['id']))->select();
			$vo=current($list);
			$this->assign('vo',$vo);
			$this->display();
		}else{
			return false;
		}
		
	}

	public function save(){
		$product=M('Product');
		$id=$_POST[id];
		$data['name']=$_POST['name'];
		$data['code']=$_POST['code'];
		
		$where['name']  = array('eq',$data['name']);
		$where['code']  = array('eq',$data['code']);
		$where['_logic'] = 'or';
		$map['_complex'] = $where;
		$map['id']  = array('neq',$id);
		$r=$product->where($map)->select();

		if($r){
			$this->error('已经存在此产品名称或CODE');
		}else{
			$product->where(array('id'=>$id))->save($data);
			$this->redirect('Product/edit',array('id'=>$id),1,'编辑成功');
		}
		
	}
}

?>